<?php
/**
 * @Author: anchen
 * @Date:   2015-12-02 09:49:19
 * @Last Modified by:   anchen
 * @Last Modified time: 2017-05-04 21:02:59
 *
 */


/**
 * 检测用户是否登录
 * @return integer 0-未登录，大于0-当前登录用户ID
 */
function is_login(){
    $user = session('user_auth');
    if (empty($user)) {
        return 0;
    } else {
        return $user['uid'];
    }
}

/*获取用户名*/
function get_username(){
    $user = session('user_auth');
    if (empty($user)) {
        return 0;
    } else {
        return $user['username'];
    }
}

/*获取会员信息*/
function get_member($uid,$map=array(),$result=''){
   $member = D('Member')->where($map)->field($result)->find();
   return $member[$result];
}

/*获取管理员相关信息*/
function get_adminInfo($id = 0){
  if(empty($id)){
    $user = session('user_auth');
    $adminInfo = D('Admin')->find($user['uid']);
  }else{
    $adminInfo = D('Admin')->find($id);
  }
  return $adminInfo;
}

 /**
  * 检测输入的验证码是否正确
  * @param  string  $code 为用户输入的验证码字符串
  * @return bool
  */
 function check_verify($code, $id = ''){
     $verify = new \Think\Verify();
     return $verify->check($code, $id);
 }

  /*检测是否已登录*/
  function check_login()
  {
      $uid = is_login();
      // 还没登录 跳转到登录页面
      if(!$uid){
      header('location:/Yzadmin/public/login.html');exit;
      }
  }

  /*统计栏目下文章数量*/
  function total_count($type='',$catid=0)
  {
    if ($type=="单页"){
       $count=D('Pages')->where('catid='.$catid)->count();
    }
    if ($type=="新闻" || $type=="产品"){
       $count=D('Article')->where('catid='.$catid)->count();
    }
    if ($type=="版位"){
       $count=D('Ads')->where('catid='.$catid)->count();
    }

    return $count;

  }


    /*统计模块下信息总数*/
  function M_total_count($model='Article',$where='')
  {
    $count=D($model)->where($where)->count();
    return $count;
  }

  //获取分类名称
  function get_row($type='',$catid=0,$returnchar='')
  {
    if ($type=="栏目"){
       $return=D('Category')->where('id='.$catid)->find();
    }
    if ($type=="版位"){
       $return=D('Adstype')->where('id='.$catid)->find();
    }

    if ($type=="友链"){
       $return=D('Linkstype')->where('id='.$catid)->find();
    }

    if ($type=="留言"){
       $return=D('Messagetype')->where('id='.$catid)->find();
    }

    if ($type=="管理员组"){
       $return=D('AuthGroup')->where('id='.$catid)->find();
    }
    if ($type=="权限"){
       $return=D('AuthRule')->where('id='.$catid)->find();
    }

    return $return[$returnchar];

  }

  /**
 * 删除目录和文件
 */
function delete_dir($path = '') {
    if(is_dir($path)) {
        $file_list= scandir($path);
        foreach ($file_list as $file) {
            if( $file!='.' && $file!='..') {
                delete_dir($path.'/'.$file);
            }
        }
        rmdir($path);
    } else {
        unlink($path);
    }
}

/*跳转地址*/
function gohref($action,$href=''){
  if ($action == '-1'){
    echo '<script>history.go(-1);</script>';
  }else{
    echo '<script>window.location.href="'.$href.'"</script>';
  }
}
/**
*后台左侧栏目
*/
function get_menu_tree($pid=0){
  $auth=new \Think\Auth();
  if($pid == 0){return array();exit;}$lists_array_news = array();
  $lists_array = D('AdminNav')->where(array('pid'=>$pid,'display'=>1))->order('sort asc,id desc')->select();
  if(!empty($lists_array)){
    foreach ($lists_array as $key=>$val){
      if(empty($val['url'])){
        $lists_array[$key]['url'] = get_fistMenu_url($val['id']);
      }
      $lists_array[$key]['childs'] = get_menu_tree($val['id']);
    }
  }
  if ($_SESSION['user_auth']['uid'] != 5){
    foreach ($lists_array as $key=>$val){
      if($auth->check($val['url'],$_SESSION['user_auth']['uid'])){
        foreach ($val['childs'] as $k=>$v){
            if(!$auth->check($v['url'],$_SESSION['user_auth']['uid'])){
              unset($lists_array['childs'][$k]);
            }
        }
      }else{
        unset($lists_array[$key]);
      }
    }
  }
  return $lists_array;
}
/**
 * 获取图片
 * @param
 * @return  string
 */
function thumb($img = '', $width = 0, $height = 0) {
    if (empty($img)) {
        return __ROOT__ . '/Uploads/nopic.gif';
    }
    $Uploads = '/Uploads/';
    $file = '.' . $Uploads . $img;
    if (file_exists($file)) {
        if (empty($width)) {
            return __ROOT__ . substr($file, 1);
        } else {
            $pathinfo = pathinfo($file);
            $thumb_file = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '_' . $width . '-' . $height . '.' . $pathinfo['extension'];
            if (file_exists($thumb_file)) {
                return __ROOT__ . substr($thumb_file, 1);
            } else {
                $image = new \Think\Image();
                $image->open($file);
                if (empty($height)) {
                    $height = $image->height();
                }
                $image->thumb($width, $height,\Think\Image::IMAGE_THUMB_CENTER)->save($thumb_file);
                return __ROOT__ . substr($thumb_file, 1);
            }
        }
    }
    return __ROOT__ . '/Uploads/nopic.gif';
}

function get_nav(){
  $auth=new \Think\Auth();
  $nav_data=D('AdminNav')->where(array('pid'=>0))->field('sort,name,url,id,ico')->order('id asc')->select();$nav_data_new = array();
  /*获取下级分类是否勾选*/
  if ($_SESSION['user_auth']['uid'] != 5){
    foreach ($nav_data as $key=>$val){
      $childs_2 = D('AdminNav')->where(array('pid'=>$val['id']))->field('sort,name,url,id,ico')->order('sort asc,id desc')->select();
      foreach($childs_2 as $k=>$v){
        $childs_3 = D('AdminNav')->where(array('pid'=>$v['id']))->field('sort,name,url,id,ico')->order('sort asc,id desc')->select();
        foreach($childs_3 as $ko=>$vo){
          if($auth->check($vo['url'],$_SESSION['user_auth']['uid'])){
            $nav_data_new[$key] = $val;
            $nav_data_new[$key]['url'] = get_fistMenu_url($val['id'],1);
          }
        }
      }
    }
  }else{
    $nav_data_new = $nav_data;
  }
  /*获取下级分类是否勾选*/
  return $nav_data_new;
}

function get_fistMenu_url($pid=0,$IsNav=0){
  $auth=new \Think\Auth();$Url = '';
  if($pid == 0){return array();exit;}
  $adminNav_tree = D('AdminNav')->where(array('pid'=>$pid,'display'=>1))->order('sort asc,id desc')->select();
  if($IsNav ==1){
    foreach ($adminNav_tree as $key=>$val){
      $childs_2 = D('AdminNav')->where(array('pid'=>$val['id'],'display'=>1))->order('sort asc,id desc')->select();
      foreach($childs_2 as $k=>$v){
        if($auth->check($v['url'],$_SESSION['user_auth']['uid'])){
          $Url = $v['url'];return $Url;exit;
        }
      }
    }
  }else{
    if(!empty($adminNav_tree)){
      $lists_array =$adminNav_tree[0];
      if ($_SESSION['user_auth']['uid'] != 5){
        if($auth->check($lists_array['url'],$_SESSION['user_auth']['uid'])){
          $Url = $lists_array['url'];
        }
      }
    }
  }

  return $Url;
}
